<?php

/*
 * C订单作功能模块
 * 功能：优惠劵
 */

namespace User\Controller;

use Think\C_Controller;

class CouponController extends C_Controller {
    
    public function __construct() {
        parent::__construct();
        if($_SESSION['barber_store']){
            $this->store_id = (int)$_SESSION['barber_store'];
        }else{
            $this->store_id = (int)$_GET['store_id'];
        }
        if($this->store_id<1){
            throw new \Exception();
        }
        $s = new \Home\Logic\StoreLogic();
        $store = $s->getStore($this->store_id);
        $this->assign('logo',$store['logo']);
        $this->assign('store_name',$store['name']);
        $this->assign('store',$store);
    }

    public function index(){
        layout('layout2');
        if($_SESSION['client_id']<1){
            redirect('/user/passport/login?store_id='.$this->store_id);
        } 
        $u = new \Home\Logic\UserLogic();
        $c = new \Home\Logic\CouponLogic();
        $coupons = $c->getUserCoupon($this->store_id,$_SESSION['client_id'],0);
        if($coupons[0]==''){
           $this->assign('store_id',$this->store_id);
           $this->display('not');
            exit;
        }
        foreach($coupons as $k=>$v){
            $type = $c->getHuiById($v['coupon_id']);
            $coupons[$k]['coupon_id']=$type;
            $user = $u->getUserById($v['client_id']);
            $coupons[$k]['client_id'] = $user;
        }        
        
        $this->assign('user',$user);
        $this->assign('coupons',$coupons);
        $this->display('coupon');
    }
    
    public function append(){
        $start = (int)$_GET['start']; 
        $c = new \Home\Logic\CouponLogic();
        $u = new \Home\Logic\UserLogic();
        $coupons = $c->getUserCoupon($this->store_id,$_SESSION['client_id'],$start);
        if(count($coupons)<1){
            exit('false');
        }
        foreach($coupons as $k=>$v){
            $type = $c->getHuiById($v['coupon_id']);
            $coupons[$k]['coupon_id']=$type;
            $user = $u->getUserById($v['client_id']);
            $coupons[$k]['client_id'] = $user;
        }             
       exit(json_encode($coupons));
    }

    //查看门店优惠劵
    public function check(){ 
        /*if($_SESSION['barber_id']<1){
            redirect('/user/barber/login');
        }*/
        layout('layout2');
        $c = new \Home\Logic\CouponLogic();
        $coupons = $c->getCouponsByStore($this->store_id,0);
        if($coupons[0]==''){
           $this->assign('store_id',$this->store_id);
           $this->display('notstore');
           exit;
        }
        $s = new \Home\Logic\StoreLogic();
        $store = $s->getStore($this->store_id);
        //微信分享
        $api = import('Api2','Weixin');
        $api = new \Weixin\Api2();
        $data['jsapi_ticket'] = $api->getTicket(WEIXIN_PAY_APP_ID, WEIXIN_PAY_SECRET);
        $data['noncestr'] = 'fewgasfgadussfewo';//临时写死
        $data['timestamp'] = time();
        $data['url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        ksort($data);
        foreach($data as $k=>$v){
            $str .= $k.'='.$v.'&';
        }
        $data['sign'] = sha1(substr($str,0,-1));
        if(strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
            $this->assign('weixin',true);
        }
        $this->assign('barber_id',(int)$_SESSION['barber_id']);  
        $this->assign('data',$data); 
        $this->assign('store',$store);
        $this->assign('coupons',$coupons);
        $this->display('store');
    }
    //
    public function appendCheck(){
        $start = (int)$_GET['start'];
        $store_id = (int)$_GET['store_id'];
        $c = new \Home\Logic\CouponLogic();
        $coupons = $c->getCouponsByStore($store_id,$start);
        exit(json_encode($coupons));
    }

    //优惠劵详情
    public function detail(){
        if((int)$_GET['d']){
            $this->assign('d',(int)$_GET['d']);
        }
        $barber_id = (int)$_GET['barber_id'];
        $store_id= (int)$_GET['store_id'];
        $coupon_id = (int)$_GET['coupon_id'];
        $_SESSION['s_verify'] = time();
        $this->setSession($_SESSION);
        $c = new \Home\Logic\CouponLogic();    
        $coupon = $c->getCouponById($store_id,$coupon_id);
        if($coupon['id']<1 || $coupon['store_id'] != $store_id){
            exit;
        }
        $s = new \Home\Logic\StoreLogic();
        $store = $s->getStore($this->store_id);
        //微信分享
        $api = import('Api2','Weixin');
        $api = new \Weixin\Api2();
        $data['jsapi_ticket'] = $api->getTicket(WEIXIN_PAY_APP_ID, WEIXIN_PAY_SECRET);
        $data['noncestr'] = 'fewgasfgadussfewl';//临时写死
        $data['timestamp'] = time();
        $data['url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        ksort($data);
        foreach($data as $k=>$v){
            $str .= $k.'='.$v.'&';
        }
        $data['sign'] = sha1(substr($str,0,-1));
        if(strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
            $this->assign('weixin',true);
        }
        $this->assign('barber_id',$barber_id);   
        $this->assign('data',$data); 
        $this->assign('store',$store);
        $this->assign('coupon',$coupon);
        $this->display('detail');
    }


    //领取优惠劵
    public function ling(){
        $mobile = $_GET['mobile'];
        $code = (int)$_GET['code'];
        $data['coupon_id'] = (int)$_GET['coupon_id'];
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        if(!$m->checkCode($mobile, $code)){
            redirect('/user/coupon/detail?d=3&store_id='.(int)$_GET['store_id'].'&coupon_id='.$data['coupon_id']);
        }
        $u = new \Home\Logic\UserLogic();
        $user = $u->getUserByAccount($mobile);
        if($user ==''){
            $re = $u->createUser($mobile);
            $data['client_id'] = $re;
        }else{
            $data['client_id'] = $user['id'];
        }
        $data['number'] = $this->code();
        $data['create_time'] = time();
        $data['store_id'] = $this->store_id;
        $data['barber_id'] = (int)$_GET['barber_id'];
        if($data['coupon_id']<0 ||
            $data['store_id']<0 ||
            $data['client_id']<0 ||
            $data['barber_id']<0){

            redirect('/user/coupon/detail?d=2&store_id='.$this->store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']);
        }
        $c = new \Home\Logic\CouponLogic();

        //检查是否领取过
        $d = $c->double($data);
        if($d){
            redirect('/user/coupon/detail?d=1&store_id='.$this->store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']); //已领取过
        }
        //领取
        $s = $c->count($data['coupon_id'],$data);
        if($s == false){
            redirect('/user/coupon/detail?d=4&store_id='.$this->store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']);//领取完
        }
        if($s){   
            redirect('/user/coupon/success?store_id='.$this->store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']);
        }

    }
    //识别码
    public function code(){
        return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }


    public function success(){
        $store_id = (int)$_GET['store_id'];
        $coupon_id = (int)$_GET['coupon_id'];
        $barber_id = (int)$_GET['barber_id'];
        $s = new \Home\Logic\storeLogic();
        $store = $s->getStore($store_id);
        $this->assign('barber_id',$barber_id);
        $this->assign('coupon_id',$coupon_id);
        $this->assign('store',$store);
        $this->display('success');
    }

    //领取优惠劵
    public function ling2(){
        $mobile = $_POST['mobile'];
        if(!is_mobile($mobile)){
            exit;
        }
        $code = $_POST['code'];
        $coupon_id = explode(',',$_POST['coupon_id']);
        $store_id = (int)$_POST['store_id'];
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        if(!$m->checkCode($mobile, $code)){
            header('Location:/givingday.html?op=1&store_id='.$store_id);exit;
        }
        $u = new \Home\Logic\UserLogic();
        $user = $u->getUserByAccount($mobile);
        if($user == false){
            $re = $u->createUser($mobile);
            $data['client_id'] = $re;
        }else{
            $data['client_id'] = $user['id'];
        }
        
        $data['create_time'] = time();
        $data['store_id'] = $store_id;
        $data['barber_id'] = (int)$_POST['barber_id'];
        $index = array_rand($coupon_id);
        $data['coupon_id'] = $coupon_id[$index];
        if( $data['coupon_id']<1 ||
            $data['store_id']<1 ||
            $data['client_id']<1
            ){
            header('Location:/givingday.html?op=2&store_id='.$store_id);exit;
        }
        $c = new \Home\Logic\CouponLogic();

        //检查是否领取过
        $d = $c->double($data);
        if($d){
            header('/givingday.html?op=3&store_id='.$store_id);exit;
        }
        //领取
        foreach($coupon_id as $v){
            $data['coupon_id'] = (int)$v;
            $data['number'] = $this->code();
            if($data['coupon_id']<1){
                continue;
            }
            $s = $c->count($data['coupon_id'],$data);
        }
        if($s == false){
            header('Location:/user/coupon/detail?store_id='.$store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']);exit;//领取完
        }
        if($s){   
            header('Location:/user/coupon/success?store_id='.$store_id.'&coupon_id='.$data['coupon_id'].'&barber_id='.$data['barber_id']);exit;
        }

    }

}
